<?php
/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: dzf_base_request.php 1914 2013-08-26 03:16:11Z laoguozhang $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class dzf_base_request extends dzf_base_class
{
	public function get($k = null, $type = '', $default = null) {
		return $this->_val($_GET, $k, $type, $default);
	}

	public function get_int($k, $default = null) {
		return $this->get($k, 'd', $default);
	}


	public function get_string($k, $default = null) {
		return $this->get($k, 's', $default);
	}

	public function post($k = null, $type = '', $default = null) {
		return $this->_val($_POST, $k, $type, $default);
	}
	
	public function post_int($k, $default = null) {
		return $this->post($k, 'd', $default);
	}


	public function post_string($k, $default = null) {
		return $this->post($k, 's', $default);
	}
	
	protected function _val($source, $k = null, $type = 0, $default = null) {
		if(!isset($k)) {
			return $source;
		}
		if(isset($source[$k])) {
			switch ($type) {
				case 'd':
					$val = self::dintval($source[$k]);
					break;
				case 's':
					$val = self::dstring($source[$k]);
					break;
				case 'f':
					$val = sprintf('%F', $source[$k]);
					break;
				case '':
				default :
					$val = $source[$k];
			}
			if(empty($val) && isset($default)) {
				$val = $default;
			}
			return $val;
		} else if(isset($default)) {
			return $default;
		} else {
			return null;
		}
	}


	/**
	 * 安全的 intval， 可以支持 int(10) unsigned
	 * 支持最大整数 0xFFFFFFFF 4294967295
	 * @param mixed $int string|int|array
	 * @return mixed
	 */
	static public function dintval($int, $allowarray = false) {
		$ret = intval($int);
		if($int == $ret || !$allowarray && is_array($int)) return $ret;
		if($allowarray && is_array($int)) {
			foreach($int as &$v) {
				$v = self::dintval($v, true);
			}
			return $int;
		} elseif($int <= 0xffffffff) {
			$l = strlen($int);
			$m = substr($int, 0, 1) == '-' ? 1 : 0;
			if(($l - $m) === strspn($int,'0987654321', $m)) {
				return $int;
			}
		}
		return $ret;
	}
	
	static public function dstring($str) {
		return htmlspecialchars($str);
	}

	/**
	 * get REQUEST_URI
	 * @return string
	 */
	function request_URI() {
		if(!isset($_SERVER['REQUEST_URI'])) {
			$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
			if($_SERVER['QUERY_STRING']) {
				$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
			}
		}
		return $_SERVER['REQUEST_URI'];
	}
}